package com.music.controller;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Controller;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.web.mvc.annotation.RequestMapping;
import com.laoxuai.web.mvc.annotation.ResponseBody;
import com.music.pojo.User;
import com.music.service.UserService;
import com.music.util.HttpStatus;
import com.music.util.PasswordEncoder;
import com.music.util.PasswordUtil;
import com.music.util.Result;
import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userServiceImpl;

    /***
     * 新增
     * @param user
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(User user) {
        User user1=userServiceImpl.getUserByName(user.getName());
        if(user1!=null){
            return Result.failed("用户已存在");
        }
        int result = userServiceImpl.insert(user);
        if(result==0){
            return Result.failed("添加失败",result);
        }
        return Result.success("添加成功", result);
    }


    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Integer id) {
        int result = userServiceImpl.delete(id);
        if(result==1){
            return Result.success("删除成功");
        }else{
            return Result.failed("删除失败");
        }
    }

    /***
     * 修改
     * @param user
     * @return
     */

    @RequestMapping(value="/update")
    @ResponseBody
    public Result update(User user,HttpServletRequest request) {
        int result = userServiceImpl.update(user);
        if(result==1){
            User user1=(User)request.getSession().getAttribute("user");
            if(user1.getUserId()==user.getUserId()){
                request.getSession().setAttribute("user",user);
                return new Result(HttpStatus.UPDATED,"修改当前登录用户成功",user);
            }else{
                return Result.success("修改成功");
            }
        }else if(result==-1){
            return Result.failed("用户名已存在");
        }else{
            return Result.failed("修改失败");
        }
    }

    @RequestMapping(value="/update/profile")
    @ResponseBody
    public Result updateProfile(User user,HttpServletRequest request) {
        User user1=(User)request.getSession().getAttribute("user");
        if(user1==null){
            return Result.failed("请先登录");
        }
        user1.setName(user.getName());
        user1.setPhone(user.getPhone());
        int result = userServiceImpl.update(user1);
        if(result==1){
            request.getSession().setAttribute("user",user1);
            return Result.success("修改成功",user1);
        }else if(result==-1){
            return Result.failed("用户名已存在");
        }else{
            return Result.failed("修改失败");
        }
    }

    /***
     * 主键查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Result getByPrimaryKey(Integer id) {
        User user = userServiceImpl.getByPrimaryKey(id);
        return Result.success("查询成功", user);
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Result page(User user, int page, int size) {
        PageInfo<User> pageInfo = userServiceImpl.listPageInfo(user, page, size);
        return Result.success("查询成功", pageInfo);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(User user, int page, int size) {
        List<User> list = userServiceImpl.listPage(user, page, size);
        return Result.success("查询成功", list);
    }

    @RequestMapping("/update/password")
    @ResponseBody
    public Result updatePassword(String oldPassword,String newPassword,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        if(user==null){
            return Result.failed("请先登录");
        }
        String salt=user.getSalt();
        String password=user.getPassword();
        if(salt!=null&&password!=null){
            boolean matches=new PasswordEncoder(salt).matches(password,oldPassword);
            if(matches){
                user.setPassword(newPassword);
                if(userServiceImpl.updatePassword(user)==1){
                    request.getSession().setAttribute("user",user);
                    return Result.success("操作成功",user);
                }else{
                    return Result.failed("操作失败");
                }
            }else{
                return Result.failed("密码不正确");
            }
        }else{
            return Result.failed("操作失败");
        }
    }

    @RequestMapping("/modify/name")
    @ResponseBody
    public Result modifyName(String name,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        if(user!=null){
            if(user.getName().equals(name)){
                return Result.success("操作成功",0);
            }else{
                return userServiceImpl.getUserByName(name)!=null?Result.failed("用户名已存在",1):Result.success("操作成功",0);
            }
        }else{
            return Result.failed("请先登录",1);
        }
    }

    @RequestMapping("/modify/password")
    @ResponseBody
    public Result modifyPassword(String password,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        boolean flag=PasswordUtil.matches(user.getSalt(),password,user.getPassword());
        if(flag){
            return Result.success("操作成功 ",0);
        }else {
            return Result.failed("操作失败",1);
        }
    }
}